// @ts-ignore
/* eslint-disable */
import { request } from '@umijs/max';
import { message } from 'antd';

const getBearerAuthHeader = () => {
  const storedToken = localStorage.getItem('access_token');
  return 'bearer ' + storedToken;
};

export const getTenantId = () => {
  return localStorage.getItem('tenant_id') || '';
};

export async function baseRequest<T>(url: string, data?: any) {
  return request<T>(`/api/${url}.admin`, {
    method: 'POST',
    headers: {
      'Content-Type': 'data.d.ts/json',
      'X-Tenant-Id': getTenantId(),
      Authorization: getBearerAuthHeader(),
    },
    data,
  });
}

export async function page<T>(url: string, params: API.PageParams) {
  const { current, pageSize, ...data } = params;
  const { page }: API.PageResult = await baseRequest(`${url}/page`, {
    pageNum: current,
    pageSize: pageSize,
    data: data,
  });
  return {
    data: page?.list,
    success: true,
    total: page?.total,
  };
}

export async function list<T>(url: string, data?: any) {
  return await baseRequest<T>(`${url}/list`, data || {});
}

export async function update<T>(url: string, data: any) {
  return await baseRequest<T>(`${url}/update`, data);
}

export async function add<T>(url: string, data: any) {
  return await baseRequest<T>(`${url}/add`, data);
}

export async function remove<T>(url: string, id: string) {
  return await baseRequest<T>(`${url}/delete`, id);
}

export async function get<T>(url: string, id: string) {
  return await baseRequest<T>(`${url}/one`, id);
}

export async function getByData<T>(url: string, data: any) {
  return await baseRequest<T>(`${url}/one`, data);
}

export const handleAdd = async (url: string, fields: any) => {
  const hide = message.loading('正在添加');

  try {
    const { data }: API.BooleanResult = await add(url, { ...fields });
    hide();
    if (data) {
      message.success('添加成功');
    } else {
      message.error('添加失败');
    }
    return true;
  } catch (error) {
    hide();
    return false;
  }
};

export const handleUpdate = async (url: string, fields: any, currentRow?: any) => {
  const hide = message.loading('正在更新');
  try {
    const { data }: API.BooleanResult = await update(url, {
      ...currentRow,
      ...fields,
    });
    hide();
    if (data) {
      message.success('更新成功');
    } else {
      message.error('更新失败');
    }
    return true;
  } catch (error) {
    hide();
    return false;
  }
};

export const handleRemove = async (url: string, id: string) => {
  const hide = message.loading('正在删除');

  try {
    const { data }: API.BooleanResult = await remove(url, id);
    hide();
    if (data) {
      message.success('删除成功，即将刷新');
    } else {
      message.error('删除失败');
    }
    return true;
  } catch (error) {
    hide();
    return false;
  }
};
export const handleRemoveList = async (url: string, selectedRows: API.TableId[]) => {
  const hide = message.loading('正在删除');
  if (!selectedRows) return true;

  try {
    selectedRows.map(async (row) => await remove(url, row.id));
    hide();
    message.success('删除成功，即将刷新');
    return true;
  } catch (error) {
    hide();
    return false;
  }
};
